package com.luoran.car.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luoran.car.config.Result;
import com.luoran.car.config.UploadMinioUtils;
import com.luoran.car.domain.FaultInfo;
import com.luoran.car.service.FaultInfoService;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("fault")
public class FaultController {
    @Autowired
    FaultInfoService faultInfoService;

    @RequestMapping("list")
    public Result list(Integer current, Integer size,@RequestBody FaultInfo faultInfo){
        PageHelper.startPage(current, size);

        List<FaultInfo> list = faultInfoService.list(faultInfo);
        return Result.success(new PageInfo<>(list));
    }
    @RequestMapping("list1")
    public Result list1(){
        List<FaultInfo> list = faultInfoService.list1();
        return Result.success(list);
    }
    @RequestMapping("img")
    public String img(MultipartFile file){
        String s = UploadMinioUtils.uploadPic(file);
        return  s;
    }
    @GetMapping("excels")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("故障车辆", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<FaultInfo> list = faultInfoService.list1();
        EasyExcel.write(response.getOutputStream(), FaultInfo.class).sheet("故障车辆").doWrite(list);
    }
}



